<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>适用非变异方法</title>
</head>
<body>
    <div id="app">
        <h3>使用非变异方法</h3>
        <p>非变异方法，不改变原来的数组，但是会返回一个数组</p>
        <button @click="addfilter">filter</button>
        <button @click="addcontant">contant</button>
        <button @click="addslice">slice</button>
        <button @click="addmap">map</button>
        <button @click="addinclude">include</button>
        <button @click="addforeach">foreach</button>
    </div>
    <script language="javaScript" src="../JS文件/vue.js"></script>
    <script>
        var app=new Vue({
            el:'#app',
            data:{
                num:[1,3,2,4,6,7,8,9,6,0]
            },
            methods:{
                addfilter(){
                    console.log(this.num)
                    var gh = this.num.filter(function(number){
                        return number%2===0
                    })
                    console.log(JSON.stringify(gh))
                },
                addcontant(){
                    console.log(this.num)
                    var dd = this.num.concat(2,3,4)
                    console.log(dd)
                    var dd = this.num.concat([2,4,5],[4,6,7])
                    console.log(dd)
                },
                addslice(){
                    var dd = this.num.slice()//返回所有元素
                    console.log(dd)
                    var dd = this.num.slice(1,3)
                    console.log(dd)
                    var dd = this.num.slice(-2,2)//空值
                    console.log(dd)
                    var dd = this.num.slice(-3)//返回倒数第三个
                    console.log(dd)

                },
                addmap(){
                    console.log(this.num)
                    let dd = this.num.map(function(item,index){return item*=3})
                    console.log(dd)
                },
                addinclude(){
                    console.log(this.num)
                    let d1 = this.num.includes(5)
                    console.log(d1)
                    let d2 = this.num.includes(2)
                    console.log(d2)
                },
                addforeach(){
                    console.log(this.num)
                    this.num.forEach((item,index) => {
                        console.log(item)
                        
                    });
                },   
            }
        });
    </script>
</body>
</html>